<?php

namespace App\Http\Controllers\web;
use App\Faccades\Express\Express;
use App\Http\Controllers\Controller;
use App\Jobs\OrderJob;
use App\Repository\RepositoryInterface\OrderInterface;
use BaconQrCode\Encoder\QrCode;
use Illuminate\Http\Request;


//订单类
class OrderController extends Controller
{
    //
    protected $order;


    public function __construct(OrderInterface $order)
    {

        $this->order = $order;
    }

    //订单查询
    public function OrderInquiry(Request $request)
    {
        $data = $request->input();
        $list_data = $this->order->OrderServer($data);
        if ($list_data['code']!=200){
            return self::error('错误信息',$list_data);
        }
            return self::success('正确信息',$list_data);
    }


    //订单增加
    public function OrderAdd(Request $request)
    {

        $data = $request->input();
        $data_true = $this->order->OrderAdd($data);
        if ($data_true['code']!=200){
            return self::error('错误信息',$data_true);
        }
        $oid = $data_true['data'];
            $this->dispatch((new OrderJob($oid))
                  ->onQueue('sendOrder')
                  ->delay(now()->addMinutes(15)));
            return self::success('正确信息',$data_true);
    }

    //订单后台
    public function OrderUpdate(Request $request)
    {

        $id = $request->input('id');
        $data = $request->input('data');

        $data_true = $this->order->OrderUpData($id,$data);
        if ($data_true['code']!=200){
            return self::error('错误信息',$data_true);
        }
            return self::success('正确信息',$data_true);
    }


    //订单删除
    public function OrderDelete(Request $request)
    {
        $id = $request->input('id');
        $data_true = $this->order->OrderDelete($id);
        if ($data_true['code']!=200){
            return self::error('错误信息',$data_true);
        }
            return self::success('正确信息',$data_true);
    }


    /**
     * @param Request $request
     * 订单量 销售额统计 cqf
     */
    public function orderAndSales(Request $request){
        $cycle=$request->get('cycle','week');
        return $this->order->orderAndSales($cycle);
    }

     /**
     * 生成二维码
     * @return mixed
     */
    public function qrCode()
    {
        $order_id='tg765895639856856';
        return QrCode::encoding('UTF-8')->size(100)->generate($order_id);
    }

    /**
     * 根据订单号进行提货
     * @return array
     */
    public function pickUp()
    {
        $order_id = 'tg765895639856856';
        try {
            $result = $this->order->pickUp($order_id);
            return success('提货成功',$result);
        } catch (\Exception $exception) {
            return fails($exception->getMessage(),'');
        }

    }

}
